var did = 1;
var countdown = 60; //计时60s
var countTimer;
layui.use(['table', 'form', 'laydate'], function() {
	var table = layui.table,
		$ = layui.jquery,
		layer = layui.layer,
		form = layui.form,
		laydate = layui.laydate;

	//初始化查询参数
	var options = {
		offset: 0,
		limit: 15,
		pageIndex: 1,
	};
	var tempOptions = options;
	loadData(options);
	//点击禁用
	$(".searchTools .search-btn").on("click", function() {
		var type = $(".searchTools .type").val();
		var name = $(".searchTools .name").val();
		var datas = {
			offset: 0,
			limit: 15,
			pageIndex: 1
		};

		if(!isNull(name))
			datas['name'] = name;

		if(!isNull(type) && type != -1)
			datas['type'] = type;
		tempOptions = datas;
		loadData(datas);
	});

	$(document).keyup(function(event){
		if(event.keyCode ==13){
			$(".searchTools .search-btn").trigger("click");
		}
	});
	//点击禁用
	$(".dataSourceList").on("click", ".del-btn", function() {
		var id = parseInt($(this).attr('data-id'));
		var options = {id: id,status: 1};
		updateDataSource(options, "/console/data/source/update/status/" + id, 3);
	});

	//点击启用
	$(".dataSourceList").on("click", ".on-btn", function() {
		var id = parseInt($(this).attr('data-id'));
		var options = {id: id,status: 0};
		updateDataSource(options, "/console/data/source/update/status/" + id, 3);
	});

	//点击添加
	$(".searchTools .add-btn").on("click", function() {
		$(".addModal").show();
	})
	$(".addModal .close").on("click", function() {
		$(".addModal").hide();
		$(".addModal input").val("");
		$(".addModal select").val("");
		$(".addModal textarea").val("");
		form.render();
	})
	//点击添加提交
	$('.addModal .btn-submit').on('click', function() {
		var name = $.trim($(".addModal .name").val());
		var status = $.trim($(".addModal .status").val());
		var type = $.trim($(".addModal .type").val());
		var code = $.trim($(".addModal .code").val());
		var username = $.trim($(".addModal .username").val());
		var password = $.trim($(".addModal .password").val());
		var driver = $.trim($(".addModal .driver").val());
		var url = $.trim($(".addModal .url").val());

		var localType = $.trim($(".addModal .localType").val());
		var localUsername = $.trim($(".addModal .localUsername").val());
		var localPassword = $.trim($(".addModal .localPassword").val());
		var localDriver = $.trim($(".addModal .localDriver").val());
		var localUrl = $.trim($(".addModal .localUrl").val());
		var remark = $.trim($(".addModal .remark").val());

		if(isNull(name)) {
			layer.msg("请输入数据源名称！");
			return;
		}
		if(isNull(type)) {
			layer.msg("请选择阿里云数据源类型！");
			return;
		}
		if(isNull(code)) {
			layer.msg("请输入code码！");
			return;
		}
		if(isNull(password)) {
			layer.msg("请输入阿里云密码！");
			return;
		}
		if(isNull(username)) {
			layer.msg("请输入阿里云用户名！");
			return;
		}
		if(isNull(driver)) {
			layer.msg("请输入阿里云连接驱动！");
			return;
		}
		if(isNull(url)) {
			layer.msg("请输入阿里云连接URL！");
			return;
		}

		if(isNull(localType)) {
			layer.msg("请选择本地数据源类型！");
			return;
		}
		if(isNull(localPassword)) {
			layer.msg("请输入本地密码！");
			return;
		}
		if(isNull(localUsername)) {
			layer.msg("请输入本地用户名！");
			return;
		}
		if(isNull(localDriver)) {
			layer.msg("请输入本地连接驱动！");
			return;
		}
		if(isNull(localUrl)) {
			layer.msg("请输入本地连接URL！");
			return;
		}
		var options = {
			name: name,
			status: status,
			type: type,
			url: url,
			driver: driver,
			username: username,
			password: password,
			localType: localType,
			localUrl: localUrl,
			localDriver: localDriver,
			localUsername: localUsername,
			localPassword: localPassword,
			code: code
		};
		if(!isNull(remark))
			options['remark'] = remark;
		updateDataSource(options, "/console/data/source/save", 1);
	})
	//修改按钮
	$(".dataSourceList").on("click", ".set-btn", function() {
		$(".updateModal").show();
		did = $(this).attr('data-id');
		$(".updateModal .name").val($(this).attr("data-name"));
		$(".updateModal .remark").val($(this).attr('data-remark'));
		$(".updateModal .code").val($(this).attr("data-code"));
		$(".updateModal .type").val($(this).attr("data-type"));
		$(".updateModal .username").val($(this).attr("data-username"));
		$(".updateModal .password").val($(this).attr("data-password"));
		$(".updateModal .driver").val($(this).attr("data-driver"));
		$(".updateModal .url").val($(this).attr("data-url"));
		$(".updateModal .localType").val($(this).attr("data-localType"));
		$(".updateModal .localUsername").val($(this).attr("data-localUsername"));
		$(".updateModal .localPassword").val($(this).attr("data-localPassword"));
		$(".updateModal .localDriver").val($(this).attr("data-localDriver"));
		$(".updateModal .localUrl").val($(this).attr("data-localUrl"));
		form.render();
	})
	$(".updateModal .close").on("click", function() {
		$(".updateModal").hide();
		$(".updateModal input").val("");
		$(".updateModal select").val("");
		$(".updateModal textarea").val("");
		form.render();
	})
	//按钮修改
	$('.updateModal .btn-submit').on('click', function() {
		var id = did;
		var name = $.trim($(".updateModal .name").val());
		var status = $.trim($(".updateModal .status").val());
		var type = $.trim($(".updateModal .type").val());
		var url = $.trim($(".updateModal .url").val());
		var driver = $.trim($(".updateModal .driver").val());
		var password = $.trim($(".updateModal .password").val());
		var username = $.trim($(".updateModal .username").val());
		var localType = $.trim($(".updateModal .localType").val());
		var localUrl = $.trim($(".updateModal .localUrl").val());
		var localDriver = $.trim($(".updateModal .localDriver").val());
		var localUsername = $.trim($(".updateModal .localUsername").val());
		var localPassword = $.trim($(".updateModal .localPassword").val());
		var code = $.trim($(".updateModal .code").val());
		var remark = $.trim($(".updateModal .remark").val());
		if(isNull(name)) {
			layer.msg("请输入数据源名称！");
			return;
		}
		if(isNull(type)) {
			layer.msg("请数据源类型！");
			return;
		}
		if(isNull(code)) {
			layer.msg("请输入code码！");
			return;
		}
		if(isNull(password)) {
			layer.msg("请输入密码！");
			return;
		}
		if(isNull(username)) {
			layer.msg("请输入用户名！");
			return;
		}
		if(isNull(driver)) {
			layer.msg("请输入连接驱动！");
			return;
		}
		if(isNull(url)) {
			layer.msg("请输入连接URL！");
			return;
		}

		if(isNull(localType)) {
			layer.msg("请选择本地数据源类型！");
			return;
		}
		if(isNull(localPassword)) {
			layer.msg("请输入本地密码！");
			return;
		}
		if(isNull(localUsername)) {
			layer.msg("请输入本地用户名！");
			return;
		}
		if(isNull(localDriver)) {
			layer.msg("请输入本地连接驱动！");
			return;
		}
		if(isNull(localUrl)) {
			layer.msg("请输入本地连接URL！");
			return;
		}
		var options = {
			id: parseInt(id),
			name: name,
			status: status,
			type: type,
			url: url,
			driver: driver,
			username: username,
			password: password,
			localType: localType,
			localUrl: localUrl,
			localDriver: localDriver,
			localUsername: localUsername,
			localPassword: localPassword,
			code: code
		};
		if(!isNull(remark))
			options['remark'] = remark;

		updateDataSource(options, "/console/data/source/update/" + id, 2)
	});

	/**修改数据源**/
	function updateDataSource(options, url, type) {
		var loading = layer.load(1);
		$.ajax({
			type: "put",
			url: url,
			async: true,
			dataType: 'json',
			timeout: 5000,
			data: options,
			success: function(res) {
				layer.close(loading);
				// console.log(res);
				if(res.code == 0) {
					layer.msg(res.msg);
					if (type == 1) {
						$(".addModal").hide();
						$(".addModal input").val("");
						$(".addModal select").val("");
						$(".addModal textarea").val("");
						form.render();
					} else if (type == 2) {
						$(".updateModal").hide();
						$(".updateModal input").val("");
						$(".updateModal select").val("");
						$(".updateModal textarea").val("");
						form.render();
					}
					loadData(tempOptions);
				} else {
					layer.msg(res.msg);
					return;
				}
			},
			error: function(res) {
				layer.msg("请求失败，请稍后再试！");
				layer.close(loading);
				return;
			},
			complete: function(XMLHttpRequest, status) {
				if(status == 'timeout') {
					layer.msg("请求超时，请稍后再试！");
					layer.close(loading);
					return;
				}

			}
		});
	}

	//获取数据列表
	function loadData(options) {
		var loading = layer.load(1);
		$.ajax({
			type: "POST",
			url: "/console/data/source/list",
			async: true,
			dataType: 'json',
			timeout: 5000,
			data: options,
			success: function(res) {
				layer.close(loading);
								// console.log(res);
				if(res.code == 0) {
					$(".dataSourceListPage").page({
						total: res.data.total, //总条数
						pageIndex: options.pageIndex, //当前页
						pageSize: options.limit, //每页显示
						showInfo: true, //是否显示记录信息
						prevBtnText: '上一页',
						nextBtnText: "下一页",
						infoFormat: '总共{total}条记录,每页显示{size}条记录,共{count}页' //定义记录信息格式
					}).on("pageClicked", function(event, pageIndex) {
						//分页点击
						options.offset = options.offset - (options.pageIndex-pageIndex )* options.limit;
						options.pageIndex = pageIndex;
						loadData(options);
					});

					//具体数据列表
					var data = res.data.records;
					if(data == null || data.length <= 0) {
						$(".dataSourceList .tableList").empty().append("<tr><td colspan='10'>暂无数据</td></tr>");
						return;
					}
					var html = '';
					$.each(data, function(index, val) {
						var statusStr = val.status == 0 ? '正常' : '禁用';
						var typeStr = 'MYSQL';
						var localTypeStr = 'MYSQL';
						var statusBtn = '';
						if (val.type == 0)
							typeStr = 'MYSQL';
						else if (val.type == 1)
							typeStr = 'SQLSERVER';
						else if (val.type == 2)
							typeStr = 'ORACLE';

						if (val.localType == 0)
							localTypeStr = 'MYSQL';
						else if (val.localType == 1)
							localTypeStr = 'SQLSERVER';
						else if (val.localType == 2)
							localTypeStr = 'ORACLE';

						if (val.status == 0)
							statusBtn = '<a href="javascript:void(0)" data-id="' + val.id + '" class="layui-btn del-btn" style="background-color:#d9534f"><img src="/static/console/img/off.png" />禁用</a>';
						else
							statusBtn = '<a href="javascript:void(0)" data-id="' + val.id + '" class="layui-btn on-btn" style="background-color:#5FB878"><img src="/static/console/img/on.png" />启用</a>';
						var btn = '<a href="javascript:void(0)" data-id="' + val.id + '" data-name="'
							+ val.name + '" data-type="' + val.type + '" data-code="' + val.code + '" data-username="'
							+ val.username + '" data-password="' + val.password + '" data-url="' + val.url + '" data-remark="'
							+ val.remark + '" data-driver="' + val.driver + '" data-localUrl="' + val.localUrl + '" data-localDriver="'
							+ val.localDriver + '" data-localPassword="' + val.localPassword + '" data-localUsername="'
							+ val.localUsername + '" data-localType="' + val.localType + '" class="layui-btn set-btn" style="background-color:#5d6f97"><img src="/static/console/img/edit.png" />修改</a>' + statusBtn;

						html += "<tr><td>" + val.name + "</td><td>" + val.code + "</td><td>" + val.username + "</td><td><div class='list-text'>" + val.password + "</div></td>" +
							"<td>" + typeStr + "</td><td><div class='list-text'>" + val.driver + "</div></td><td><div class='list-text'>" + val.url
							+ "</div></td><td>" + val.localUsername + "</td><td><div class='list-text'>" + val.localPassword + "</div></td><td>" + localTypeStr
							+ "</td><td><div class='list-text'>" + val.localDriver + "</div></td><td><div class='list-text'>" + val.localUrl +
							"</div></td><td>" + statusStr + "</td><td><div class='list-text'>" + val.remark + "</div></td><td>" + btn + "</td></tr>";
					});
					$(".dataSourceList .tableList").empty().append(html);
					replaceNull($(".dataSourceList .tableList"));
				} else {
					layer.msg(res.msg);
					return;
				}
			},
			error: function(res) {
				layer.msg("请求失败，请稍后再试！");
				layer.close(loading);
				return;
			},
			complete: function(XMLHttpRequest, status) {
				if(status == 'timeout') {
					layer.msg("请求超时，请稍后再试！");
					layer.close(loading);
					return;
				}

			}
		});

	}

});